package com.lizi.idgenerator.dto;


import java.io.Serializable;

/**
 * 结果集
 * @author kingman
 * @since 2019/3/28 14:48
 * @modified by:
 */
public class ResultModel<T> implements Serializable {

    /**
     * 是否成功
     */
    private boolean success;
    /**
     * 返回消息
     */
    private String message;
    /**
     * 消息码
     */
    private int code;
    /**
     * 返回的结果
     */
    private T returnValue;

    private ResultModel() {

    }

    public ResultModel<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public ResultModel<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public int getCode() {
        return code;
    }

    public ResultModel<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public T getReturnValue() {
        return returnValue;
    }

    public ResultModel<T> setReturnValue(T returnValue) {
        this.returnValue = returnValue;
        return this;
    }

    /**
     * 是否成功
     *
     * @return
     */
    public boolean isSuccess() {
        return (success && code == 1);
    }

    /**
     * 返回成功的结果
     *
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ResultModel<T> successResult(T result) {
        ResultModel<T> resultModel = new ResultModel<T>();
        resultModel.setSuccess(true);
        resultModel.setCode(1);
        resultModel.setReturnValue(result);

        return resultModel;
    }

    /**
     * 返回失败的结果
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> ResultModel<T> failResult(int code, String msg) {
        ResultModel<T> resultModel = new ResultModel<T>();
        resultModel.setSuccess(false);
        resultModel.setCode(code);
        resultModel.setMessage(msg);

        return resultModel;
    }

}
